﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ES_Project.Model;
using System.Text;
using System.Data;
using System.Web.Configuration;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace ES_Project.Pages
{
    public partial class WebForm30 : System.Web.UI.Page
    {
        string StrConn = WebConfigurationManager.ConnectionStrings["essystem"].ToString();
        MySqlCommand objCmd = new MySqlCommand(); 
        private DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }
        protected void CMD_CLEAR_Click(object sender, EventArgs e)
        {
            clsRegCode.Text = "";
            clsName.Text = "";
            level.Text = "";
            //CMD_INSERT.Enabled = true;
            //CMD_EDIT.Enabled = false;
        }
        
        protected void CMD_SEARCH_Click(object sender, EventArgs e)
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            String strSQL;
            String condi = " where 1=1 ";

            if (!clsRegCode.Text.Equals(""))
            {
                condi = condi + " and crm.cls_reg_code = '" + clsRegCode.Text + "'";

            }
            if (!clsName.Text.Equals(""))
            {
                condi = condi + " and crm.cls_name = '" + clsName.Text + "'";

            }
            if (!level.Text.Equals(""))
            {
                condi = condi + " and crm.level = '" + level.Text + "'";

            }

            strSQL = "select crm.cls_reg_id, crm.cls_reg_code, crm.cls_name, crm.level " +
                     " from cls_register_mast crm  " + condi + " ";           

                      

                        Conn.Open();
                        MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
                        DataSet ds = new DataSet();
                        da4.Fill(ds, "cls_register_mast");

                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            GridView4.DataSource = ds.Tables["cls_register_mast"];
                            GridView4.DataBind();

                        }
                        else
                        {
                            GridView4.DataSource = null;
                            GridView4.DataBind();
                            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert1", "alert('ไม่พบข้อมูล')", true);
                        }
                
            
        }
        protected void GridView4_SelectedIndexChanged(object sender, EventArgs e)
        {
            //CMD_INSERT.Enabled = false;
            //CMD_EDIT.Enabled = true;
            using (essystemEntities context = new essystemEntities())
            {
                List<cls_register_mast> crm = context.cls_register_mast.Where("it.[cls_reg_id] = " + ((HiddenField)GridView4.SelectedRow.Cells[1].FindControl("HiddenField4")).Value.ToString() + "").ToList();
                if (crm.Count > 0) 
                {
                    clsRegId.Text = crm[0].cls_reg_id.ToString();
                    clsRegCode.Text = crm[0].cls_reg_code;
                    clsName.Text = crm[0].cls_name;
                    level.Text = crm[0].level;
                }
            }
        }
        protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                ImageButton ib = (ImageButton)e.Row.FindControl("ImageButton1");
                ib.Attributes.Add("onclick", "return confirm('คุณต้องการที่จะลบข้อมูล?');");

            }
            HyperLink edit = (HyperLink)(e.Row.FindControl("edit"));
            if (edit != null)
            {
                // HyFO4.Text = (string)DataBinder.Eval(e.Row.DataItem, "AgentCode");
                edit.ImageUrl = "~/Pages/images/icon_15.gif";
                edit.NavigateUrl = "Master_Subject.aspx?clsRegId=" + (string)DataBinder.Eval(e.Row.DataItem, "cls_reg_id").ToString();
                //edit.ImageUrl = "images.gif";
                //HyFO4.NavigateUrl = "#";
                //HyFO4.Attributes.Add("Onclick", "opennewsletter('Pay_Course.aspx?emp_id=" + (string)DataBinder.Eval(e.Row.DataItem, "emp_id").ToString() + "');");
                //+ "&TxtAO=" + TxtAO.Text + "&TxtAgent=" + TxtAgent.Text + "&TxtInvoice1=" + TxtInvoice1.Text + "&TxtInvoice2=" + TxtInvoice2.Text + "&TxtDate1=" + TxtDate1.Text + "&TxtDate2=" + TxtDate2.Text + "&StatusWork=" + StatusWork.SelectedValue;
            }
        }

        protected void GridView4_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            strSQL = "DELETE FROM cls_register_mast WHERE cls_reg_id ='" + GridView4.DataKeys[e.RowIndex].Value + "'";
            Conn.Open();
            objCmd = new MySqlCommand(strSQL, Conn);

            objCmd.ExecuteNonQuery();


            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert3", "alert('ลบข้อมูลเรียบร้อยแล้ว')", true);
            GridView4.EditIndex = -1;
            GridView4.DataSource = null;
            GridView4.DataBind();
            clsRegCode.Text = "";
            clsName.Text = "";
            level.Text = "";
            //BindData();

        }
    }
}